
#include "MapView.h"
#include "Application.h"
#include "hgeUtil.h"

//
// Constructor
//

MapView::MapView()
{
	map = new Map(this);

	// Background
	InitQuad(background, hge->Texture_Load("data/starfield.png"));
	SetQuadRect(background, 0, 0, ScreenWidth(), ScreenHeight());
	SetQuadRect(guiQuad, 0, ScreenHeight()-200, ScreenWidth(), ScreenHeight());
	SetQuadTexCo(background, 0, 0, 2*ScreenWidth()/1920.0f, ScreenHeight()/1200.0f);

	// Gui quad
	InitQuad(guiQuad, guiBackground, 0xB0FFFFFF);
	SetQuadTexCo(guiQuad);
}

//
// Destructor
//

MapView::~MapView()
{
	hge->Texture_Free(background.tex);
	delete map;
}

//
// Enter
//

void MapView::Enter()
{
}

//
// Exit
//

void MapView::Exit()
{
	map->Clear();
}

//
// Process
//

View *MapView::Process()
{
	const float dt = hge->Timer_GetDelta();

	if(ProcessGui(dt))
		return 0;

	if(ProcessInput(dt))
		return 0;

	return this;
}

//
// ProcessGui
//

bool MapView::ProcessGui(const float dt)
{
	return false;
}

//
// ProcessInput
//

bool MapView::ProcessInput(const float dt)
{
	if(hge->Input_GetKeyState(HGEK_ESCAPE))
		return true;

	// React to the mouse
	float x, y;
	hge->Input_GetMousePos(&x, &y);

	const float scale = ScreenWidth() / (viewArea.x2 - viewArea.x1);
	const float scrollSpeed = ScreenHeight() * 0.7f * dt / scale;
	const float scrollArea = 10; //ScreenHeight() * 0.1;

	// Scroll
	if(x < scrollArea || (hge->Input_GetKeyState(HGEK_LEFT) && !hge->Input_GetKeyState(HGEK_CTRL)))
	{ viewArea.x1 -= scrollSpeed; viewArea.x2 -= scrollSpeed; }
	else if(x > ScreenWidth()-scrollArea || (hge->Input_GetKeyState(HGEK_RIGHT) && !hge->Input_GetKeyState(HGEK_CTRL)))
	{ viewArea.x1 += scrollSpeed; viewArea.x2 += scrollSpeed; }

	if(y < scrollArea || (hge->Input_GetKeyState(HGEK_UP) && !hge->Input_GetKeyState(HGEK_CTRL)))
	{ viewArea.y1 -= scrollSpeed; viewArea.y2 -= scrollSpeed; }
	else if(y > ScreenHeight()-scrollArea || (hge->Input_GetKeyState(HGEK_DOWN) && !hge->Input_GetKeyState(HGEK_CTRL)))
	{ viewArea.y1 += scrollSpeed; viewArea.y2 += scrollSpeed; }

	// Zoom
	float wheel = (float)hge->Input_GetMouseWheel();
	if(hge->Input_GetKeyState(HGEK_UP) && hge->Input_GetKeyState(HGEK_CTRL))
		wheel = 50 * dt;
	else if(hge->Input_GetKeyState(HGEK_DOWN) && hge->Input_GetKeyState(HGEK_CTRL))
		wheel = -50 * dt;
	if(wheel)
	{
		const float zoom = 1.0f - wheel * 0.03f;

		float w = 0.5f*(viewArea.x2 - viewArea.x1);
		float h = 0.5f*(viewArea.y2 - viewArea.y1);

		const float x = viewArea.x1 + w;
		const float y = viewArea.y1 + h;

		w *= zoom;
		h *= zoom;

		viewArea.x1 = x - w;
		viewArea.x2 = x + w;
		viewArea.y1 = y - h;
		viewArea.y2 = y + h;
	}

	return false;
}

//
// Render
//

void MapView::Render()
{
	// Render the map first then the gui over it
	RenderMap(false);
	RenderGui();

	// Render the cursor
	RenderCursorSprite();
}

//
// RenderMap
//

void MapView::RenderMap(bool drawLines)
{
	// Draws a star field for a background
	hge->Gfx_RenderQuad(&background);

	// Render the map
	map->Render(viewArea, drawLines);
}

//
// RenderGui
//

void MapView::RenderGui()
{
	// Render gui
	hge->Gfx_RenderQuad(&guiQuad);
}

//
// InitializeViewArea
//

void MapView::InitializeViewArea()
{
	viewArea.Set(0, 0, ScreenWidth()*2, ScreenHeight()*2);
}
